\ concurrent VI VI for Laura's expt - dependant
\ two mags, weighing

\outputs
^Llight = 1
^lmag   = 2
^beep   = 3
^Rlight = 4
^rmag   = 5

\inputs
\R1 - left key
\R2 - right key
\R3 - left sensor
\R4 - right sensor

\VI lists
\list o is the sequence of VIs ...a VI 15 (before I change it to another one)
\list w are the numbers 0-14 that med picks to start from

list o = 1, 5, 17, 21, 3, 7, 19, 23, 9, 13, 25, 29, 11, 15, 27
list w = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14

\event dat will be recorded in c array
\    each event will be coded.... 1100030 will be a true left peck at 3 seconds
\                                 12....COD left peck
\                                 13....left rft start
\                                 14... left rft end
\                                 15... left mag beam broken
\                                 16....left mag beam no longer broken
\                                 18....left forced choice peck
\                                 19....left peck - light off right forced choice time
\                                 21....true right peck (true = not in COD)
\                                 22....COD right peck
\                                 23....right rft start
\                                 24....right rft end
\                                 25....right mag beam broken
\                                 26....right mag beam no longer broken
\                                 28....right forced choice peck
\                                 29....right peck - light off left forced choice time

dim c=10000
dim u=3000 \left and right mag weights
dim v=3000


diskvars = c,u,v
diskformat = 9.1

dim e = 21  \whole day data

\Pecking data
\e(0) = left VI
\e(1) = right VI
\e(2) = left feed (1=wheat, 2=puffed wheat, 3=pellets)
\e(3) = right feed
\e(4) = time to first peck
\e(5) = pecks left
\e(6) = pecks right
\e(7) = time left    - seconds
\e(8) = time right   - seconds
\e(9) = rft left
\e(10) = rft right
\e(11)= post left rft pause
\e(12)= post right rft pause
\e(13)= number of COs L>R
\e(14)= number of COs R>L
\e(15)= COD pecks L>R  (right pecks)
\e(16)= COD pecks R>L  (left pecks)
\e(17)= cum eat time left
\e(18)= cum eat time right
\e(19)= total time
\e(20)= amount eaten left mag
\e(21)= amount eaten right mag

\variable list
\a - used at start to keep track of forced choice
\b - extra variable array
dim b=20
\b(0) - counting COD
\b(1) - total rft, including forced choice
\b(2) - rft count flag for VI stuff
\b(3) - counting eat time
\b(4) - getting mag weight stabilities
\b(5) - getting mag weight stabilities
\b(6) - COD
\c - event data array
\d - used by background procedures
\e - summary data array
\f - used by weighing backprocs - from BKGRND 7 - left mag - com2
\g - used by weighing backprocs - from BKGRND 8 - right mag - com1
\h - used by weighing backprocs - for stable weights at start and finish
\i - used by weighing backprocs - for stable weights at start and finish
\j - current place in VI list for Left VI
\k - current place in VI list for Right VI
\l - Left VI
\m - L VI multiplier
\n - R VI multiplier
\o - VI list
\p - used to calculate proportions
\q - showing states of eat time for trouble shooting
\r - Right VI
dim s=1
\s(0) - left mag start weight
\s(1) - right mag start weight
\t - counting time in tenths of secs for event data
\u - left mag weights
\v - right mag weights
\w - list for getting random start point for VI
\x - current spot in c array
\y - current spot in u array
\z - current spot in v array

\z1 - start of session - issued by mag weighing state set
\                      - also end of forced choice
\z2 - end of session

\------------------------------------------------------------------------------------------
S.S.1,

s1,
  #z1:show 1,Hen,animal number;
      set e(0)=60, e(1)=60,       \e(0)=left VI, e(1)=right VI  *********change VI here
          m=e(0)/15, n=e(1)/15,   \work out the multipliers to give correct VI from VI15 list
          e(2)=1,e(3)=1,          \e(2)=left feed, e(3)=right feed**********change feed info
          b(6)=20,                  \******COD in tenths of seconds
          c(x)=9999999; add x;
          randd j=w;randd k=w;      \get random starting places from VI list
          list L=o(j);list R=o(k);  \get the first VIs from the list
          set L=L*m, R=R*n;         \multiply to make correct VI from VI 15 list
          withpi=5000 [@left,@right]
             @left:on ^Llight--->s20
             @right:on ^Rlight--->s21  \go to forced choice

\...........................................................................
\forced choice of FR 1 on each side at the start of the session

s20,  \left light on
  #z2:off 1,2,3,4,5--->s1
  #R1:off ^Llight;on ^beep,^Lmag;set a=1,c(x)=18000000+t;add b(1),x;
        show 24,Total Rfts,b(1)--->s22
  #R2:set c(x)=29000000+t;add x--->sx

s21, \right light on
  #z2:off 1,2,3,4,5--->s1
  #R2:off ^Rlight;on ^beep,^Rmag;set a=2,c(x)=28000000+t;add b(1),x;
        show 24,Total Rfts,b(1)--->s22
  #R1:set c(x)=19000000+t;add x--->sx

s22,
  #z2:off 1,2,3,4,5--->s1
  0.05":off^beep--->s23

s23,
  #z2:off 1,2,3,4,5--->s1
  2.5":off ^Lmag,^Rmag;
     if b(1)<2 [@cont,@stop]
       @cont:if a=1 [@doneL,@doneR]
         @doneL:on ^Rlight--->s21
         @doneR:on ^Llight--->s20
       @stop:on^Llight,^Rlight;set a=0;z1--->s2

\...........................................................................
\program proper

s2,  \time to first response
  #z2:off 1,2,3,4,5--->s1
  #R1:on^beep--->s3
  #R2:on^beep--->s4
  0.1":set e(4)=e(4)+0.1--->sx

s3,   \dealing with a left peck
  #z2:off 1,2,3,4,5--->s1
  0.05":off^beep;add e(5);set p=(e(5)/(e(5)+e(6)))*1000;
        show 8,Pecks L,e(5),18,Prop L,p;
        if b(0)>=b(6) [@OK,@COD]
            @OK:set c(x)=11000000+t;add x;
                if L>0 [@no rft,@rft]
                   @norft:--->s5
                   @rft:set c(x)=13000000+t;add x,e(9),b(1);
                        show 33,Rft L,e(9),24,Total Rfts,b(1);
                        off^Llight,^Rlight;on^Lmag;--->s7
            @COD:set c(x)=12000000+t;add x,e(16);--->s5

s4,    \dealing with a right peck
  #z2:off 1,2,3,4,5--->s1
  0.05":off^beep;add e(6);set p=(e(5)/(e(5)+e(6)))*1000;
        show 13,Pecks R,e(6),18,Prop L,p;
        if b(0)>=b(6) [@OK,@COD]
            @OK:set c(x)=21000000+t;add x;
                if R>0 [@no rft,@rft]
                   @norft:--->s6
                   @rft:set c(x)=23000000+t;add x,e(10),b(1);
                        show 38,Rft R,e(10),24,Total Rfts,b(1);
                        off^Llight,^Rlight;on^Rmag;--->s8
            @COD:set c(x)=22000000+t;add x,e(15);--->s6

s5,  \working on left
  #z2:off 1,2,3,4,5--->s1
  #R1:on^beep--->s3
  #R2:set b(0)=0;add e(13);
      on^beep--->s4  \b(0)=COD counter, counts continuously in s.s.2

s6,   \working on right
  #z2:off 1,2,3,4,5--->s1
  #R2:on^beep--->s4
  #R1:set b(0)=0;add e(14);
      on^beep--->s3

s7,  \left rft
  #z2:off 1,2,3,4,5--->s1
  2.5":off^Lmag;set c(x)=14000000+t;add x;
     if b(1)<32 [@cont,@stop]
       @cont:list L=o(j); set L=L*m;show 55,L VI,L; on^Llight,^Rlight--->s9
       @stop:z2--->s1

s8,  \right rft
  #z2:off 1,2,3,4,5--->s1
  2.5":off^Rmag;set c(x)=24000000+t;add x;
     if b(1)<32 [@cont,@stop]
       @cont:list R=o(k); set R=R*n;show 60,R VI,R; on^Llight,^Rlight--->s10
       @stop:z2--->s1

s9,  \ post left-rft pause
  #z2:off 1,2,3,4,5--->s1
  #R1:on^beep--->s3
  #R2:set b(0)=0;add e(13);
      on^beep--->s4
  0.1":set e(11)=e(11)+0.1--->sx


s10,  \post right rft pause
  #z2:off 1,2,3,4,5--->s1
  #R2:on^beep--->s4
  #R1:set b(0)=0;add e(14);
      on^beep--->s3
  0.1":set e(12)=e(12)+0.1--->sx

\------------------------------------------------------------------------------------------
S.S.2,   \counting session time, COD etc

s1,
  #z1:--->s2

s2,
  #z2:--->s1
  0.1":add t,b(0),b(3);set e(19)=e(19)+0.1;
       show 29,Total Time,e(19);
       if e(19)<2400 [@cont,@stop]
          @cont:--->sx
          @stop:z2;--->s1

\------------------------------------------------------------------------------------------
S.S.3,   \time working on left and right

s1,
  2#z1:--->s2

s2,  \before first resp, not working on either side yet
  #z2:--->s1
  #R1:--->s3
  #R2:--->s4

s3, \working on left, count here until there's a right peck
  #z2:--->s1
  #R2:--->s4
  0.1":set e(7)=e(7)+0.1;show 23,Time L,e(7)--->sx

s4, \working on right
  #z2:--->s1
  #R1:--->s3
  0.1":set e(8)=e(8)+0.1;show 28,Time R,e(8)--->sx
\-------------------------------------------------------------------------------------------
S.S.4,   \organising the VIs

s1,
  2#z1:--->s2

s2,
  #z2:--->s1
  1":sub L,R; show 55,L VI,L, 60,R VI,R;
       if (L>0) and (R>0) [@cont,@rftdue]
          @cont:--->sx
          @rftdue:set b(2)=b(1)--->s3   \set b(2) to total rft count so far

s3,
  #z2:--->s1
  0.1":if b(1)=b(2) [@wait,@rft]  \once there's been another rft
        @wait:--->sx
        @rft:--->s4    \if b(2) & b(1) are different then there's been another rft

s4,  \rft happening dont do anything
  #z2:--->s1
  3":if (L>0) and (R>0) [@cont,@rftdue]  \were there 2 rfts due at the same time?
          @cont:--->s2
          @rftdue:set b(2)=b(1)--->s3

\--------------------------------------------------------------------------------------------
 S.S.5,  \mag sensor stuff for every beam break regardless of mag op

s1,
  2#z1:--->s2

s2,
  #z2:--->s1
  #R3:set c(x)=15000000+t;add x--->s3  \left sensor broken
  #R4:set c(x)=25000000+t;add x--->s4  \right sensor broken

s3,  \head in left mag at x time
  #z2:--->s1
  #R3:--->s3
  0.1":set c(x)=16000000+t-1;add x--->s2

s4,  \head in right mag time
  #z2:--->s1
  #R4:--->s4
  0.1":set c(x)=26000000+t-1;add x--->s2
\-------------------------------------------------------------------------
S.S.6,   \counting time with head in mag...
         \every time the beam is broken will be recorded and then the time
         \when it ceases to be broken.  The start and finish times
         \of the mag op are also recorded (earlier) so you can get eat
         \times from this...
s1,
  2#z1:set q=2--->s2

s2,
  #z2:--->s1
  0.05":show 53,L Eat Time,e(17),58,R Eat Time,e(18);
        if (s.s.1=7) [@lrft,@not]
            @lrft:--->s3
            @not: if (s.s.1=8)[@rrft,@not]
                    @rrft:--->s5
                    @not:--->sx
\..........................................................................
s3,  \left rft wait for beam break
  #z2:--->s1
  #R3:set b(3)=0--->s4  \beam broken
  0.1":if (s.s.1=7) [@still rft,@over]
            @still rft:--->sx
            @over:--->s2

s4,
  #z2:--->s1
  #R3:if (s.s.1=7) [@still rft,@over]
            @still rft:--->s4    \while the beam is still broken do nothing
            @over:set e(17)=e(17)+(b(3)/10);show 53,L Eat Time,e(17);--->s2
  0.1":set e(17)=e(17)+((b(3)-1)/10);show 53,L Eat Time,e(17);
       if (s.s.1=7) [@still rft,@over]
            @still rft:--->s3
            @over:--->s2
\..........................................................................
s5,  \right rft wait for beam break
  #z2:--->s1
  #R4:set b(3)=0;--->s6  \beam broken
  0.1":if (s.s.1=8) [@still rft,@over]
            @still rft:--->sx
            @over:--->s2

s6,
  #z2:--->s1
  #R4:if (s.s.1=8) [@still rft,@over]
            @still rft:--->s6  \while the beam is still broken do nothing
            @over:set e(18)=e(18)+(b(3)/10);show 58,R Eat Time,e(18);--->s2
  0.1":set e(18)=e(18)+((b(3)-1)/10);show 58,R Eat Time,e(18);
       if (s.s.1=8) [@still rft,@over]
            @still rft:--->s5
            @over:--->s2

\----------------------------------------------------------------------------
S.S.7,   \the mag weighing stuff

s1,
  #start:set u(y)=1111111,v(z)=2222222;add y,z;
         BKGRND 7;BKGRND 8;--->s12

s12,
  1":BKGRND 7;BKGRND 8;
      if f=h [@leftsame,@not]
           @leftsame:add b(4);
              if g=i [@rightsame,@not]
                 @rightsame:add b(5);--->s14
                 @not:set b(5)=0,i=g--->s14
           @not:set b(4)=0,h=f; if g=i [@rightsame,@not]
                 @rightsame:add b(5);--->s14
                 @not:set b(5)=0,i=g--->sx
s14,
  0":show 51,L mag stable?,b(4),56,R mag stable?,b(5);
         if (b(4)>=5) AND (b(5)>=5) [@stable,@not]
              @stable:z1;set s(0)=f,s(1)=g--->s2
             @not:--->s12

s2,
  0":show 15,L Mag Start,s(0),20,R Mag start,s(1);
         set u(y)=(e(9)+1)*100000+f,v(z)=(e(10)+1)*100000+g;
         add y,z;--->s4

s4,
  #z2:set h=f,i=g,b(4)=0,b(5)=0--->s5
  1":BKGRND 7; BKGRND 8;
     set u(y)=(e(9)+1)*100000+f,v(z)=(e(10)+1)*100000+g,
     e(20)=s(0)-f ,e(21)=s(1)-g;
     show 43,Amount Eaten L,e(20), 48,Amount Eaten R,e(21),
       35,L Mag Weight,f, 40,R Mag Weight,g;
     add y,z; --->sx

s5,  \session over wait until both mags stable for final reading before saving data
     \as everything else is stopped we can re-use most of the variables
  1":BKGRND 7;BKGRND 8;
     set u(y)=(e(9)+1)*100000+f,v(z)=(e(10)+1)*100000+g;add y,z;
     show 35,L Mag Weight,f, 40,R Mag Weight,g;
     if f=h [@leftsame,@not]
           @leftsame:add b(4);
              if g=i [@rightsame,@not]
                 @rightsame:add b(5);--->s6
             @not:set b(5)=0--->s6
           @not:set b(4)=0; if g=i [@rightsame,@not]
                 @rightsame:add b(5);--->s6
             @not:set b(5)=0,h=f,i=g--->sx
s6,
  0":show 51,L mag stable?,b(4),56,R mag stable?,b(5);
     if (b(4)>=5) AND (b(5)>=5) [@stable,@not]
        @stable:set e(20)=s(0)-f, e(21)=s(1)-g,d=99;
             show 43,Amount Eaten L,e(20), 48,Amount Eaten R,e(21);
             BKGRND 2;--->s7
      @not:set h=f,i=g--->s5

s7,
  0.1":if d=1 [set c(x)=123456789,x=x+1,c(x)=-987.987,
               u(y)=12345678,y=y+1,u(y)=-987.987,
               v(z)=1234567,z=z+1,v(z)=-987.987]--->stopabortflush

       \then stop and save event data























































